import { Component, OnInit, Input, Output, EventEmitter, OnChanges, SimpleChanges, DoCheck, OnDestroy } from '@angular/core';

@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.css']
})
export class OrderComponent implements OnInit, OnChanges, DoCheck, OnDestroy {

  private order: Order;
  private number: number = 100;
  private price: number = 100;
  @Output()
  private priceChange: EventEmitter<PriceQuote> = new EventEmitter<PriceQuote>();
  @Input()
  private stockCode: string="";
  private oldStockCode: string;
  private stockChanged: boolean = false;
  private stockChanges: number = 0;
  constructor() {
    //this.order = new Order(this.stockCode,100 );
    setInterval(() => {
      this.price = 100 * Math.random();
      this.priceChange.emit(new PriceQuote("IBM", this.price));
    }, 3000);
  }
  ngOnInit() {

  }
  ngDoCheck(): void {
    if (this.oldStockCode != this.stockCode) {
      this.stockChanged = true;
      console.log(`stockeCode从${this.oldStockCode}变为${this.stockCode}`);
      this.oldStockCode = this.stockCode;
    }
    if (this.stockChanged) {
      this.stockChanges = 0;
    } else {
      this.stockChanges++;
      console.log(`没有改变的次数${this.stockChanges}`);
    }
  }
  ngOnChanges(changes: SimpleChanges): void {
    console.log(JSON.stringify(changes, null, 2));
  }
  ngOnDestroy(): void {
    console.log('order destroyed.');
  }
}
export class Order {
  constructor(public stockCode: string,
    public number: number) { }
}
export class PriceQuote {
  constructor(public stockCode: string, public price: number) { }
}
